<?php
// src/Pyaf/GymBundle/Repository/AvisoFaltaRepository.php
namespace Pyaf\GymBundle\Repository;

use Doctrine\ORM\EntityRepository;
use Pyaf\GymBundle\Entity\AvisoFalta;
/**
 * Description of AvisoFaltaRepository
 *
 * @author Toshiba P755-S5272
 */
class AvisoFaltaRepository extends EntityRepository{
    
    /**
     *  
     * @return a list of Profesores order by name
     */
    public function findAllOrderByNombre() {        
        return $this->getEntityManager()
                ->createQuery('SELECT af FROM PyafGymBundle:AvisoFalta af ORDER BY af.fechaFalta ASC')                
                ->getResult();
    }
    
    public function getFaltasAlumno($id) {
        date_default_timezone_set('America/Argentina/Buenos_Aires');
        $currentDay = date("Y-m-d 00:00:00");
        return $this->getEntityManager()
                ->createQuery("SELECT af FROM PyafGymBundle:AvisoFalta af WHERE af.alumno = $id
                               AND af.fechaFalta > '$currentDay'")
                ->getResult();
    }
    
    public function getRecuperarAlumno($id) {
        date_default_timezone_set('America/Argentina/Buenos_Aires');
        $currentDay = date("Y-m-d 00:00:00");
        return $this->getEntityManager()
                ->createQuery("SELECT af FROM PyafGymBundle:AvisoFalta af WHERE af.alumnoRecupera = $id
                               AND af.fechaFalta > '$currentDay'")
                ->getResult();
    }
    
    public function getFechaRecuperar($id){
        return $this->getEntityManager()
                    ->createQuery("SELECT af FROM PyafGymBundle:AvisoFalta af WHERE af.id = $id")
                    ->getOneOrNullResult();
    }

    public function save($avisoFalta){
        $this->saveAvisoFalta($avisoFalta);
    }
    
    public function saveAvisoFalta($avisoFalta) {
        $em = $this->getEntityManager();
        $em->persist($avisoFalta);
        $em->flush();
    }
    
    public function deleteDay($id){
        $day = new AvisoFalta();
        $day = $this->getEntityManager()->find('PyafGymBundle:AvisoFalta', $id);
        
        $em = $this->getEntityManager();
        $em->remove($day);
        $em->flush();
    }
    
    public function retrieveAllFreeDays(){
        date_default_timezone_set('America/Argentina/Buenos_Aires');
        $currentDay = date("Y-m-d 00:00:00");
        return $this->getEntityManager()
                ->createQuery("SELECT af FROM PyafGymBundle:AvisoFalta af WHERE af.libre = true
                               AND af.fechaFalta > '$currentDay'")
                ->getResult();
    }
    
    public function retrieveAllDaysEmptyAlumno(){
        date_default_timezone_set('America/Argentina/Buenos_Aires');
        $currentDay = date("Y-m-d 00:00:00");
        return $this->getEntityManager()
                ->createQuery("SELECT af FROM PyafGymBundle:AvisoFalta af WHERE af.alumno is NULL
                               AND af.fechaFalta >= '$currentDay'")
                ->getResult();
    }
    
    public function findAllCurrentDate($currentDate) {
        return $this->getEntityManager()
                ->createQuery("SELECT af FROM PyafGymBundle:AvisoFalta af WHERE af.fechaFalta = '$currentDate'")
                ->getResult();
    }
    
    public function findAllFijosCurrentDate($currentDate){
        return $this->getEntityManager()
                ->createQuery("SELECT af FROM PyafGymBundle:AvisoFalta af WHERE af.alumno is NULL
                               AND af.fechaFalta >= '$currentDate'")
                ->getResult();
    }
    
    public function findAllAlumnosAvisaronClaseFecha($fecha) {
        return $this->getEntityManager()
                ->createQuery("SELECT af FROM PyafGymBundle:AvisoFalta af 
                               WHERE af.fechaFalta = '$fecha'
                               AND af.alumno IS NOT NULL")
                ->getResult();
    }
    
    public function findAllAlumnosRecuperanClaseFecha($fecha) {
        return $this->getEntityManager()
                ->createQuery("SELECT af FROM PyafGymBundle:AvisoFalta af 
                               WHERE af.fechaFalta = '$fecha'
                               AND af.alumnoRecupera IS NOT NULL")
                ->getResult();
    }
}

?>
